package com.share.common.utils;

import com.github.javafaker.Faker;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/12/15 18:58
 */
public class MockUtil {
    public static Faker faker = new Faker(Locale.CHINA);

    /*返回中文名字*/
    public static String userName() {
        return faker.name().fullName();
    }

    @ApiOperation("返回座机电话")
    public static String mobilePhone() {
        return faker.phoneNumber().phoneNumber();
    }

    @ApiOperation("返回手机号")
    public static String telePhone() {
        return faker.phoneNumber().cellPhone();
    }

    @ApiOperation("返回街道地址")
    public static String streetAddress() {
        return faker.address().streetAddress();
    }

    @ApiOperation("返回城市")
    public static String country() {
        return faker.country().name();
    }

    /*返回ipV4*/
    public static String ipV4() {
        return faker.internet().ipV4Address();
    }

    /*返回ipV6*/
    public static String ipV6() {
        return faker.internet().ipV6Address();
    }

    /*统一社会信用代码*/
    public static String creditCode() {
        return CreditCodeUtil.getCreditCode();
    }

    @ApiOperation("生产背面图片,并上传到minio")
    public static String genIdCardBackImage(String idCard) {
        String path = getImagePath(idCard, "back.png");
        byte[] bytes = new byte[0];
        try {
            bytes = IdCardUtil.genBackImage(idCard);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return MinioUtil.uploadFile(path, bytes);
    }

    @ApiOperation("生产正面图片,并上传到minio")
    public static String genIdCardFrontImage(String name, String idCard, String address) {
        String path = getImagePath(idCard, "front.png");
        byte[] bytes = new byte[0];
        try {
            bytes = IdCardUtil.genFrontImage(name, idCard, address);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return MinioUtil.uploadFile(path, bytes);
    }

    @ApiOperation("pdf文件填充---生成pdf授权书")
    public static String genPersonAuthorizationFile(String templateUrl, String name, String idCard, String signTime) {
        String path = "/pdf/" + DateExtUtil.getDate() + "/" + DateExtUtil.getTime() + "_" + idCard + "_" + (System.currentTimeMillis() + ".pdf").substring(10);
        byte[] bytes = new byte[0];
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("name", name);
            param.put("idCard", idCard);
            param.put("signTime", signTime);
            bytes = PdfUtil.fill(templateUrl, param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return MinioUtil.uploadFile(path, bytes);
    }

    @ApiOperation("pdf文件填充---生成pdf文件")
    public static String fillPdfFile(String templateUrl, Map<String, Object> fillParam) {
        String path = "/pdf/" + DateExtUtil.getDate() + "/" + DateExtUtil.getTime() + "_" + (System.currentTimeMillis() + ".pdf").substring(10);
        byte[] bytes = new byte[0];
        try {
            bytes = PdfUtil.fill(templateUrl, fillParam);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return MinioUtil.uploadFile(path, bytes);
    }

    private static String getImagePath(String idCard, String subFix) {
        String path = "/image/" + DateExtUtil.getDate() + "/" + DateExtUtil.getTime() + "_" + idCard + "_" + subFix;
        return path;
    }
}
